<?php

declare(strict_types=1);

namespace App\Model;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id 
 * @property string $email 
 * @property string $password 
 * @property string $pic 
 * @property string $nickname 
 * @property int $create_time 
 */
class User extends \App\Model\Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'users';

    public const CREATED_AT = 'create_time';
    public const UPDATED_AT = null;

    protected ?string $dateFormat = 'U';
    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['email', 'password', 'pic', 'nickname'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'create_time' => 'integer'];


    /**
     * 获取用户详情
     * @param $id
     * @return array
     */
    public function getUserInfo($id)
    {
        $userInfo = User::query()->where('id', $id)->select('id', 'email', 'pic', 'create_time', 'nickname')->first();
//        if (empty($userInfo)) {
//            throw new BusinessException((ErrorCode::USER_NOT_EXISTS));
//        }

        return empty($userInfo) ? null : $userInfo->toArray();
    }

    /**
     * @param array $ids
     * @return mixed[]
     */
    public function getUserInfos(array $ids)
    {
        return User::query()->whereIn('id', $ids)->select('id', 'email', 'pic', 'create_time', 'nickname')->get()->toArray();
    }
}
